// Tierless
// Copyright © 2010 Alphora
// This file is licensed under a modified BSD-license (see tierless_license.txt).
using System;

using Tierless.Framework.EntityTypes;

namespace Tierless.Framework.Entities
{
	public class StringColumn : EntityColumn
	{
		public StringColumn(Entity entity, EntityTypeColumn columnType, int columnIndex)
			: base(entity, columnType, columnIndex)
		{}

		protected override object InternalSetValue(object tempValue)
		{
			if (tempValue == null || tempValue == DBNull.Value)
				return null;

			var stringValue = tempValue as string;
			if (stringValue == null)
				throw new ArgumentException("Value must be set to a valid string value.");

			return stringValue.Length == 0 ? null : tempValue;
		}

		protected override object InternalConvertValue(object tempValue)
		{
			return Convert.ToString(tempValue);
		}

		protected override TypeCode InternalGetTypeCode()
		{
			return TypeCode.String;
		}

		protected override bool InternalIsEmpty()
		{
			return AsString == String.Empty;
		}

		public override string AsString
		{
			get { return base.AsString; }
			set { SetValue(value); }
		}
	}
}